
#include "SSXCQuote.h"
#include <core/strings/string_util.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
//#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// class CXCQuote

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXCQuoteRep::CXCQuoteRep()
{
}

CXCQuoteRep::~CXCQuoteRep()
{
	Clear( );
}

bool CXCQuoteRep::Clear( )
{
	std::lock_guard<std::mutex> locker(m_lock);
	m_RepMap.m_map.clear();
	return true;
}

bool CXCQuoteRep::InsertQuote(const XCQUOTE& newElement )
{
	std::string strMarket= newElement.m_sListedMarket;
	std::lock_guard<std::mutex> locker(m_lock);
	if(strMarket.length() > 0){
		XCQUOTE item;
		memset(&item, 0, sizeof(XCQUOTE));
		item = m_RepMap.m_map[strMarket];
		if(strlen(item.m_sBondKey) <= 0){
			m_RepMap.m_map[strMarket] = newElement;
		}
		else{
			if(strlen(newElement.m_OfrPriceYield) <= 0 && strlen(newElement.m_OfrPriceClean) <= 0){
				FIELDCOPY(item.m_BidPriceYield,newElement.m_BidPriceYield);
				FIELDCOPY(item.m_BidPriceClean,newElement.m_BidPriceClean);
				FIELDCOPY(item.m_BidVol,newElement.m_BidVol);
			}
			else if(strlen(newElement.m_BidPriceYield) <= 0 && strlen(newElement.m_BidPriceClean) <= 0){
				FIELDCOPY(item.m_OfrPriceYield,newElement.m_OfrPriceYield);
				FIELDCOPY(item.m_OfrPriceClean,newElement.m_OfrPriceClean);
				FIELDCOPY(item.m_OfrVol,newElement.m_OfrVol);
			}
			else return false;
			item.m_time = newElement.m_time;
			m_RepMap.m_map[strMarket] = item;
		}
		return true;
	}
	return true;
}

bool CXCQuoteRep::GetQuoteByKey(const std::string& sKey,XCQUOTE &stRep)
{
	std::lock_guard<std::mutex> locker(m_lock);
	if(m_RepMap.m_map.size()==0)return false;
	std::map<std::string, XCQUOTE> ::iterator itr = m_RepMap.m_map.find(sKey);
	if(itr!=m_RepMap.m_map.end()){
		stRep = itr->second;
		return true;
	}
	return false;
}

int CXCQuoteRep::GetSize() const
{ 
	std::lock_guard<std::mutex> locker(const_cast<std::mutex&>(m_lock));
	return m_RepMap.m_map.size(); 
}
const char* CXCQuoteRep::GetName()const{
	return "CXCQuoteRep";
}
int	CXCQuoteRep::GetBytes(int mode)const
{
	int bytes = 0;
	std::lock_guard<std::mutex> locker(const_cast<std::mutex&>(m_lock));
	if (mode == 0)
	{
		bytes = (sizeof(std::map<std::string, XCQUOTE>::value_type) - sizeof(std::string))*m_RepMap.m_map.size();
		std::map<std::string, XCQUOTE>::const_iterator it = m_RepMap.m_map.begin();
		for (; it != m_RepMap.m_map.end(); ++it)
		{
			bytes += qb::base::StringUtil::GetStringAllBytes(it->first);
		}
	}
	else
	{
		bytes = sizeof(XCQUOTE)*m_RepMap.m_map.size();
		std::map<std::string, XCQUOTE>::const_iterator it = m_RepMap.m_map.begin();
		for (; it != m_RepMap.m_map.end(); ++it)
		{
			bytes += qb::base::StringUtil::GetStringRealBytes(it->first);
		}
	}
	return bytes;
}

